﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IBupro.Classes;
using iBuproModel.Repositories;
using IBupro.Classes.services;
using iBuproModel.Repositories.Interfaces;

namespace IBupro.Controllers.Classes
{
    public class CustomController : Controller, ICustomController
    {
        protected IRepositoryService repositoryService;
        protected IMailer mailer;

        protected IFlashMessenger FlashMessenger { get; private set; }

        protected Translator Translator { get; private set; }

        public IUserRepository users;
        public IDiplomProjectRepository diplomProjects;
        public IDepartmentRepository departments;

        public CustomController()
        {
            this.repositoryService = new RepositoryService();
            this.FlashMessenger = new FlashMessanger();
            this.Translator = Translator.Instance;
            this.mailer = MailerService.GetMailerInstance();


            users = repositoryService.UserRepositoryCreateInstance();
            diplomProjects = repositoryService.DiplomProjectRepositoryCreateInstance();
            departments = repositoryService.DepartmentRepositoryCreateInstance();
        }

        #region InitCulture

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            this.InitCulture(requestContext);
            base.Initialize(requestContext);
        }
        
        public string CurrentCulture
        {
            get
            {
                return Translator.Instance.Locale;
            }
        }

        private void InitCulture(RequestContext requestContext)
        {
            string cultureCode = GetCulture(requestContext.HttpContext);

            requestContext.HttpContext.Response.Cookies.Add(
                new HttpCookie("Culture", cultureCode)
                {
                    Expires = DateTime.Now.AddYears(1),
                    HttpOnly = true,
                }
            );

            Translator.Instance.Locale = cultureCode;

            CultureInfo culture = new CultureInfo(cultureCode);
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
        }

        private string GetCulture(HttpContextBase context)
        {
            string code = GetCookieCulture(context);

            if (string.IsNullOrEmpty(code))
            {
                code = GetCountryCulture(context);
            }

            return code;
        }

        private string GetCookieCulture(HttpContextBase context)
        {
            HttpCookie cookie = context.Request.Cookies["Culture"];

            if (cookie == null || string.IsNullOrEmpty(cookie.Value) ||
                 !Translator.Cultures.Contains(cookie.Value))
            {
                return string.Empty;
            }

            return cookie.Value;
        }

        private string GetCountryCulture(HttpContextBase context)
        {
            // some GeoIp magic here
            return "cs-CZ";
        }

        #endregion


    }
}
